package org.springblade.mach;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.springblade.core.tool.node.INode;
import org.springblade.mach.entity.DrawingInfoTreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LY
 * @version 1.0
 * @date 2021/4/13 15:07
 */
public class DrawingInfoNodeManager<T extends DrawingInfoNodeInteface<T>> {
	private final ImmutableMap<String, T> nodeMap;
	private final Map<Long, Object> parentIdMap = Maps.newHashMap();

	public DrawingInfoNodeManager(List<T> nodes) {

		this.nodeMap = Maps.uniqueIndex(nodes, DrawingInfoNodeInteface::getKeyId);
	}

	public DrawingInfoNodeInteface getTreeNodeAt(long parentId) {
		for (String s : nodeMap.keySet()) {
			String[] split = s.split("-");
			long v = Long.parseLong(split[1]);
			if (v == parentId) {
				return (DrawingInfoTreeNode) this.nodeMap.get(s);
			}
		}
		return null;
	}

	public void addParentId(Long parentId) {
		this.parentIdMap.put(parentId, "");
	}

	public List<T> getRoot() {
		List<T> roots = new ArrayList();
		this.nodeMap.forEach((key, node) -> {
			if (node.getParentId() == 0L || this.parentIdMap.containsKey(node.getKeyId())) {
				roots.add(node);
			}

		});
		return roots;
	}


}
